package com.mao.action.organization;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.mao.action.BaseAction;
import com.mao.model.organization.Organization;
import com.mao.model.organization.OrganizationClass;
import com.mao.service.impl.organization.OrganizationCondition;
import com.mao.service.organization.IOrganizationClassService;
import com.mao.service.organization.IOrganizationService;
import com.mao.util.SessionHelper;

public class OrganizationAction extends BaseAction {
	private static final long serialVersionUID = 1L;

	private Organization o;

	private OrganizationCondition organizationCondition;
	
	private IOrganizationService<Organization> organizationService;
	
	private IOrganizationClassService<OrganizationClass> organizationClassService;

	private Map<String, Object> result;

	private String orgClassId = "0";

	private Integer page;

	private Integer rows;
	
	private String orgName;
	
	private String contactPerson;
	
	

	@Override
	public String execute() {
		return SUCCESS;
	}

	public String jsonSaveOrganization() {
		o.setStore(SessionHelper.getSessionUser().getStore());
		organizationService.saveOrganization(o);
		return SUCCESS;
	}

	public String jsonGetOrgsByOrgClass() {
		if(organizationCondition == null)
			organizationCondition = new OrganizationCondition();
		
		organizationCondition.setName(orgName);
		organizationCondition.setContactPerson(contactPerson);
		organizationCondition.setStoreId(SessionHelper.getSessionUser().getStore().getId());

		if(orgClassId != "0"){
			OrganizationClass orgClass = organizationClassService.getOrganizationClassById(new Long(orgClassId));
			organizationCondition.setTreePath(orgClass.getTreePath());
		}
		result = new HashMap<String, Object>();
		result.put("total" , organizationService.getOrgsByOrgClassgCount(organizationCondition,Long.valueOf(orgClassId)));
		result.put("rows", organizationService.getOrgsByOrgClass(organizationCondition,Long.valueOf(orgClassId),page,rows));
		return SUCCESS;
	}

	public String jsonUpdateOrganization() {
		Organization updateOrg = organizationService.getOrganizationById(o.getId());
		OrganizationClass oc = null;
		if (o.getOc().getId() != null) {
			oc = organizationClassService.getOrganizationClassById(o.getOc().getId());
			updateOrg.setOc(oc);
		}
		
		updateOrg.setName(o.getName());
		updateOrg.setBankNo(o.getBankNo());
		updateOrg.setContactPerson(o.getContactPerson());
		updateOrg.setPhone(o.getPhone());
		updateOrg.setFax(o.getFax());
		updateOrg.setEmail(o.getEmail());
		updateOrg.setZipCode(o.getZipCode());
		updateOrg.setAddress(o.getAddress());
		updateOrg.setNote(o.getNote());
		
		organizationService.updateOrganization(updateOrg);
		return SUCCESS;
	}
	
	public String deleteOrg(){
		o = organizationService.getOrganizationById(o.getId());
		o.setStatus(Organization.STATUS_DELETED);
		organizationService.updateOrganization(o);
		return SUCCESS;
	}

	
	
	public Organization getO() {
		return o;
	}

	public void setO(Organization o) {
		this.o = o;
	}

	public IOrganizationService<Organization> getOrganizationService() {
		return organizationService;
	}

	public void setOrganizationService(IOrganizationService<Organization> organizationService) {
		this.organizationService = organizationService;
	}

	public Map<String, Object> getResult() {
		return result;
	}

	public void setResult(Map<String, Object> result) {
		this.result = result;
	}

	public String getOrgClassId() {
		return orgClassId;
	}

	public void setOrgClassId(String orgClassId) {
		this.orgClassId = orgClassId;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public Integer getRows() {
		return rows;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
	}

	public OrganizationCondition getOrganizationCondition() {
		return organizationCondition;
	}

	public void setOrganizationCondition(OrganizationCondition organizationCondition) {
		this.organizationCondition = organizationCondition;
	}

	public String getOrgName() {
		return orgName;
	}

	public void setOrgName(String orgName) {
		this.orgName = orgName;
	}

	public String getContactPerson() {
		return contactPerson;
	}

	public void setContactPerson(String contactPerson) {
		this.contactPerson = contactPerson;
	}

	public IOrganizationClassService<OrganizationClass> getOrganizationClassService() {
		return organizationClassService;
	}
	@Autowired
	public void setOrganizationClassService(
			IOrganizationClassService<OrganizationClass> organizationClassService) {
		this.organizationClassService = organizationClassService;
	}
	
	

}
